package com.qfjy.config.example5;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserDelayedConfig
 * @Description TODO  用户管理模块 延迟队列
 * @Author guoweixin
 * @Date 2022/4/2
 * @Version 1.0
 */
@Configuration
public class UserDelayedConfig {
    /**
     * TODO  1、声明交换机   delayed.exchange.user.register
     *       2、声明队列     delayed.queue.user.register
     *       3、路由key     delayed.key.user.register
     *       4、交换机和队列 路由KEY 绑定
     */

    /**
     * TODO 1、声明交换机
     */
    @Bean
    public CustomExchange delayedExchangeUserRegister(){
        /*
          TODO String name 交换机名称
               String type 交换机类型
               boolean durable 是否持久化（默认在内存，保证数据不丢失 true)
               boolean autoDelete (是否删除 false)
               Map<String, Object> arguments 队列消息（删除条件）【自定义 延迟队列】
         */

        Map<String, Object> arguments=new HashMap<>();
         arguments.put("x-delayed-type","direct");

        return new CustomExchange("delayed.exchange.user.register","x-delayed-message",
                true,false,arguments);
    }
    /**
     * TODO  2、声明队列     delayed.queue.user.register
     */
    @Bean
    public Queue delayedQueueUserRegister(){
        return  new Queue("delayed.queue.user.register");
    }
    /**
     * TODO 3、绑定 交换机和路由和队列
     */
    @Bean
    public Binding bindDelayedQueueExchangeUserRegister(Queue delayedQueueUserRegister,
                                                        CustomExchange delayedExchangeUserRegister){
        return BindingBuilder.bind(delayedQueueUserRegister).
                to(delayedExchangeUserRegister).with("delayed.key.user.register").noargs();
    }

}
